package com.hk.service.impl.kyc;

import java.util.List;

import org.springframework.stereotype.Service;

import com.hk.commons.Constant;
import com.hk.commons.HqlFilter;
import com.hk.commons.entity.Card;
import com.hk.dao.base.impl.BaseServiceImpl;
import com.hk.service.kyc.ICard;


@Service
public class CardImpl extends BaseServiceImpl<Card> implements ICard {

	@Override
	public Card findById(Integer id) {
		Card result = null;
		if (id != null){
			result = getById(id);
		}
		return result;
	}

	@Override
	public Boolean add(Card model) {
		 
		model.setCts(System.currentTimeMillis());
		super.save(model);
		return true;
	}
	
	@Override
	public List<Card> getActiveCard(){
		HqlFilter hqlFilter = new HqlFilter();
		hqlFilter.addEQFilter("status", Constant.ACTIVITY_ACTIVE);
		List<Card> actList = findByFilter(hqlFilter);
		return actList;
	}
	
	
	@Override
	public Boolean checkActive(Integer cardId){
		Boolean result = true;
		Card card = findById(cardId);
		
		Long currentTime = System.currentTimeMillis();
		
		if(card != null){
			//检查活动开始结束时间是否在有效期内
			if (currentTime.compareTo(card.getStartDate()) > 0 
					&& currentTime.compareTo(card.getEndDate()) < 0 ){
				if(card.getTotalNum() <= card.getUsedNum()){
					result = false;
				}
			}else{
				result = false;
			}
		}else{
			result = false;
		}
		return result;
	}
}
